﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FIFP.Common;
using FIFP.DashBoard.Model;
using System.Collections.ObjectModel;
using System.ComponentModel;
using FIFP.DashBoard.Helper;
using Telerik.Windows.Controls;
using System.Collections.Generic;
using Telerik.Windows.Controls.ScheduleView;
using System.Linq;

namespace FIFP.DashBoard.ViewModel
{
    public class OverlapVM : ViewModelCore
    {
        #region Variables
        private List<FIFPAppointment> allPlans;

        private string selectedUnitType;
        private ObservableCollection<OverlapGroup> overlapGroups;
        private OverlapGroup selectedOverlapGroup;
        private ObservableCollection<FIFPAppointment> selectedAppointments = new ObservableCollection<FIFPAppointment>();
        private List<FIFPAppointment> checkedAppointments = new List<FIFPAppointment>();
        private List<FIFPAppointment> checkedSimposAppointments = new List<FIFPAppointment>();
        private List<SimopsAppliedGroup> simopsAppliedGroups = new List<SimopsAppliedGroup>();
        private SimopsAppliedGroup selectedSimopsAppliedGroup;
        private int selectedIndex1 = -1;
        private int selectedIndex2 = -1;
        
        private bool isApplySimopsButtonEnabled;
        private bool isCancelSimopsButtonEnabled;
        private bool isCheckOverlapButtonEnabled;
        private bool isRemoveOverlapButtonEnabled;
        private DataGrid overlapGroupGrid;             
        
        private Dictionary<int, FIFPAppointment> activityDictionary = new Dictionary<int, FIFPAppointment>();
        public event EventHandler<CustomEventArgs<AppointmentGroup>> OnAppointmentDataCompleted;

        #endregion

        #region Properties
        public DataGrid OverlapGroupGrid
        {
            get { return overlapGroupGrid; }
            set { SetPropertyValue(() => OverlapGroupGrid, value, val => overlapGroupGrid = val); }
        }

        public ObservableCollection<OverlapGroup> OverlapGroups
        {
            get { return overlapGroups; }
            set
            {
                if (overlapGroups != value)
                {
                    overlapGroups = value;
                    OnPropertyChanged(() => this.OverlapGroups);
                }
            }
        }

        public ObservableCollection<FIFPAppointment> SelectedAppointments
        {
            get { return selectedAppointments; }
            set
            {
                if (selectedAppointments != value)
                {
                    selectedAppointments = value;
                    OnPropertyChanged(() => this.SelectedAppointments);
                }
            }
        }

        public List<FIFPAppointment> CheckedAppointments
        {
            get { return checkedAppointments; }
            set
            {
                if (checkedAppointments != value)
                {
                    checkedAppointments = value;
                    OnPropertyChanged(() => this.CheckedAppointments);
                }
            }
        }

        public bool IsApplySimopsButtonEnabled
        {
            get { return isApplySimopsButtonEnabled; }
            set { SetPropertyValue(() => IsApplySimopsButtonEnabled, value, val => isApplySimopsButtonEnabled = val); }
        }

        public bool IsCancelSimopsButtonEnabled
        {
            get { return isCancelSimopsButtonEnabled; }
            set { SetPropertyValue(() => IsCancelSimopsButtonEnabled, value, val => isCancelSimopsButtonEnabled = val); }
        }

        public bool IsCheckOverlapButtonEnabled
        {
            get { return isCheckOverlapButtonEnabled; }
            set { SetPropertyValue(() => IsCheckOverlapButtonEnabled, value, val => isCheckOverlapButtonEnabled = val); }
        }

        public bool IsRemoveOverlapButtonEnabled
        {
            get { return isRemoveOverlapButtonEnabled; }
            set { SetPropertyValue(() => IsRemoveOverlapButtonEnabled, value, val => isRemoveOverlapButtonEnabled = val); }
        }


        private Style _GridStyle = Application.Current.Resources[FIFPConstants.GRID_STYLE] as Style;
        public Style GridStyle
        {
            get { return _GridStyle; }
            set { SetPropertyValue(() => GridStyle, value, val => _GridStyle = val); }
        }

        private Style _GridHeaderStyle = Application.Current.Resources[FIFPConstants.GRID_HEADER_STYLE] as Style;
        public Style GridHeaderStyle
        {
            get { return _GridHeaderStyle; }
            set { SetPropertyValue(() => GridHeaderStyle, value, val => _GridHeaderStyle = val); }
        }

        private Style _GridRowStyle = Application.Current.Resources[FIFPConstants.GRID_ROW_STYLE] as Style;
        public Style GridRowStyle
        {
            get { return _GridRowStyle; }
            set { SetPropertyValue(() => GridRowStyle, value, val => _GridRowStyle = val); }
        }

        private Style _GridCellStyle = Application.Current.Resources[FIFPConstants.GRID_CELL_STYLE] as Style;
        public Style GridCellStyle
        {
            get { return _GridCellStyle; }
            set { SetPropertyValue(() => GridCellStyle, value, val => _GridCellStyle = val); }
        }

        private Style _OverlapButtonStyle = Application.Current.Resources[FIFPConstants.OVERLAP_BUTTON_STYLE] as Style;
        public Style OverlapButtonStyle
        {
            get { return _OverlapButtonStyle; }
            set { SetPropertyValue(() => OverlapButtonStyle, value, val => _OverlapButtonStyle = val); }
        }

        private List<string> unitTypeList = new List<string>();
        public List<string> UnitTypeList
        {
            get { return unitTypeList; }
            set { SetPropertyValue(() => UnitTypeList, value, val => unitTypeList = val); }
        }

        #endregion

        #region Command

        private ICommand _OverlapGroupLoadCommand;
        public ICommand OverlapGroupLoadCommand
        {
            get
            {
                return _OverlapGroupLoadCommand ??
                        (_OverlapGroupLoadCommand = new RelayCommand<DataGrid>(InitializeOverlapGroup));
            }
        }

        private ICommand _OverlapGroupSelectChange;
        public ICommand OverlapGroupSelectChange
        {
            get
            {
                return _OverlapGroupSelectChange ??
                        (_OverlapGroupSelectChange = new RelayCommand<object>(OverlapGroupGridSelectionChange));
            }
        }

        private ICommand _OverlapActivityCheckedCommand;
        public ICommand OverlapActivityCheckedCommand
        {
            get
            {
                return _OverlapActivityCheckedCommand ??
                        (_OverlapActivityCheckedCommand = new RelayCommand<CheckBox>(OverlapActivityChecked));
            }
        }

        private ICommand _OverlapActivityUnCheckedCommand;
        public ICommand OverlapActivityUnCheckedCommand
        {
            get
            {
                return _OverlapActivityUnCheckedCommand ??
                        (_OverlapActivityUnCheckedCommand = new RelayCommand<CheckBox>(OverlapActivityUnChecked));
            }
        }

        private ICommand _SimopsActivityCheckedCommand;
        public ICommand SimopsActivityCheckedCommand
        {
            get
            {
                return _SimopsActivityCheckedCommand ??
                        (_SimopsActivityCheckedCommand = new RelayCommand<CheckBox>(SimposActivityChecked));
            }
        }

        private ICommand _SimopsActivityUnCheckedCommand;
        public ICommand SimopsActivityUnCheckedCommand
        {
            get
            {
                return _SimopsActivityUnCheckedCommand ??
                        (_SimopsActivityUnCheckedCommand = new RelayCommand<CheckBox>(SimposActivityUnChecked));
            }
        }

        private ICommand _ApplySimopsCommand;
        public ICommand ApplySimopsCommand
        {
            get
            {
                return _ApplySimopsCommand ??
                        (_ApplySimopsCommand = new RelayCommand<Button>(ApplySimopsClicked));
            }
        }

        private ICommand _CancelSimopsCommand;
        public ICommand CancelSimopsCommand
        {
            get
            {
                return _CancelSimopsCommand ??
                        (_CancelSimopsCommand = new RelayCommand<Button>(CancelSimopsClicked));
            }
        }

        private ICommand _CheckSelectedOverlapGroupCommand;
        public ICommand CheckSelectedOverlapGroupCommand
        {
            get
            {
                return _CheckSelectedOverlapGroupCommand ??
                        (_CheckSelectedOverlapGroupCommand = new RelayCommand<Button>(CheckSelectedOverlapGroup));
            }
        }

        //private ICommand _RemoveSelectedOverlapGroupCommand;
        //public ICommand RemoveSelectedOverlapGroupCommand
        //{
        //    get
        //    {
        //        return _RemoveSelectedOverlapGroupCommand ??
        //                (_RemoveSelectedOverlapGroupCommand = new RelayCommand<Button>(RemoveSelectedOverlapGroup));
        //    }
        //}

        private ICommand _OverLapCheckingCommand;
        public ICommand OverLapCheckingCommand
        {
            get
            {
                return _OverLapCheckingCommand ??
                        (_OverLapCheckingCommand = new RelayCommand<Button>(OverLapChecking));
            }
        }

        private ICommand _CheckedUnitTypeEvent;
        public ICommand CheckedUnitTypeEvent
        {
            get
            {
                return _CheckedUnitTypeEvent ??
                        (_CheckedUnitTypeEvent = new RelayCommand<RadioButton>(UnitTypeFilter));
            }
        }


        #endregion

        #region Constructor
        public OverlapVM()
        {
            ServiceLocator.Get<MainPageVM>().OnOverlapClick += new EventHandler(OverlapVM_OnOverlapClick);
            LoadUnitType();
            getAllPlans();
            //LoadData();           
            IsApplySimopsButtonEnabled = false;
            IsCancelSimopsButtonEnabled = false;
            IsCheckOverlapButtonEnabled = false;
            isRemoveOverlapButtonEnabled = false;
        }

        void OverlapVM_OnOverlapClick(object sender, EventArgs e)
        {
            if (SelectedAppointments != null)
            {
                SelectedAppointments.Clear();
                LoadOverlapPlans(SelectedAppointments);
            }
        }

        #endregion

        #region Events Methods 

        private void InitializeOverlapGroup(DataGrid layoutRoot)
        {
            if (SelectedAppointments != null && SelectedAppointments.Count == 0)
            {
                overlapGroupGrid = layoutRoot;
                LoadOverlapPlans(new ObservableCollection<FIFPAppointment>());
            }
            
        }

        //unit type radial button selected
        private void UnitTypeFilter(RadioButton chkUnitType)
        {
            string unitType = Convert.ToString(chkUnitType.Tag);
            if (!string.IsNullOrEmpty(unitType))
            {
                if (Convert.ToBoolean(chkUnitType.IsChecked))
                {
                    selectedUnitType = unitType;
                }
            }
        }

        //single unit overlap checking button  clicked
        private void OverLapChecking(Button btnOverlapChecking)
        {
            if (btnOverlapChecking.Name == "btnOverlapCheckingOnEachUnit")
                OverlapCheckingOnSingleUnit();           
            else if (btnOverlapChecking.Name == "btnOverlapCheckingOnTowerWell")
                OverlapCheckingOnTowerWells();
            else if (btnOverlapChecking.Name == "btnOverlapCheckingOnTowerPipeline")
                OverlapCheckingOnTowerPipelines();
            else if (btnOverlapChecking.Name == "btnOverlapCheckingOnWellPipeline")
                OverlapCheckingOnWelPipelines();
            else if (btnOverlapChecking.Name == "btnOverlapCheckingOnAlfaTower")
                OverlapCheckingOnAlfaTower();
            //else if ( btnOverlapChecking.Name == "btnOverlapCheckingOnAlfaTowerWells")
            //    OverlapCheckingOnAlfaTowerWells();
            
        }       

        //overlap unit grid selection change
        private void OverlapGroupGridSelectionChange(object e)
        {           
            
            int index = overlapGroupGrid.SelectedIndex;
            if (index >= 0)
            {
                IsCheckOverlapButtonEnabled = true;
                IsApplySimopsButtonEnabled = false;
                IsCancelSimopsButtonEnabled = false;
                IsRemoveOverlapButtonEnabled = false;
                
                selectedOverlapGroup = (OverlapGroup)overlapGroupGrid.SelectedItem;                
                List<FIFPAppointment> SelectedAppointmentsList = new List<FIFPAppointment>();

                if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_SINGLEUNIT)
                {                 
                   SelectedAppointmentsList = (from item in allPlans where item.UnitName == selectedOverlapGroup.OverlapLocation
                                                select item).ToList<FIFPAppointment>();
                }               
                else if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_TOWERWELLS)
                {
                    int i = selectedOverlapGroup.OverlapLocation.IndexOf("|");
                    string name1 = selectedOverlapGroup.OverlapLocation.Substring(0, i);
                    string name2 = selectedOverlapGroup.OverlapLocation.Substring(i + 1);

                    SelectedAppointmentsList = (from item in allPlans
                                                where item.UnitName == name1 || item.UnitName == name2
                                                select item).ToList<FIFPAppointment>();
                }
                else if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_TOWERPIPELINES)
                {
                    int i = selectedOverlapGroup.OverlapLocation.IndexOf("|");
                    string name1 = selectedOverlapGroup.OverlapLocation.Substring(0, i);
                    string name2 = selectedOverlapGroup.OverlapLocation.Substring(i + 1);

                    SelectedAppointmentsList = (from item in allPlans
                                                where item.UnitName == name1 || item.UnitName == name2
                                                select item).ToList<FIFPAppointment>();
                }
                else if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_WELLPIPELINES)
                {
                    int i1 = selectedOverlapGroup.OverlapLocation.IndexOf("|");
                    int i2 = selectedOverlapGroup.OverlapLocation.LastIndexOf("|");
                    string name1 = selectedOverlapGroup.OverlapLocation.Substring(0, i1);
                    string name2 = selectedOverlapGroup.OverlapLocation.Substring(i1 + 1, i2 - i1-1);
                    string name3 = selectedOverlapGroup.OverlapLocation.Substring(i2+1);                  

                    SelectedAppointmentsList = (from item in allPlans
                                                where item.UnitName == name1 || item.UnitName == name2 || item.UnitName == name3
                                                select item).ToList<FIFPAppointment>();

                    foreach (FIFPAppointment p in SelectedAppointmentsList)
                    {
                        if (p.UnitType == FIFPConstants.UNITTYPE_TOWER)
                            p.IsOverlapped = false;
                    }
                }
                else if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_ALFATOWER)
                {
                    int i = selectedOverlapGroup.OverlapLocation.IndexOf("|");
                    string name1 = selectedOverlapGroup.OverlapLocation.Substring(0, i);
                    string name2 = selectedOverlapGroup.OverlapLocation.Substring(i + 1);

                    SelectedAppointmentsList = (from item in allPlans
                                                where item.UnitName == name1 || item.UnitName == name2
                                                select item).ToList<FIFPAppointment>();
                }
                //else if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_ALFATOWERWELLS)
                //{
                //    int i = selectedOverlapGroup.OverlapLocation.IndexOf("(A)|");
                //    string name1 = selectedOverlapGroup.OverlapLocation.Substring(0, i);
                //    string name2 = selectedOverlapGroup.OverlapLocation.Substring(i + 4);

                //    SelectedAppointmentsList = (from item in allPlans
                //                                where item.UnitName == name1 ||( item.UnitName == name1 + "A" )|| item.UnitName == name2
                //                                select item).ToList<FIFPAppointment>();
                //}

                SelectedAppointments = new ObservableCollection<FIFPAppointment>(SelectedAppointmentsList);
                LoadOverlapPlans(SelectedAppointments);
                //CheckedAppointments.Clear();
            }
        }

        private void CheckSelectedOverlapGroup(Button btnSelectedCheckOverlapGroup)
        {
            if (SelectedAppointments != null)
            {
                //pre check
                List<FIFPAppointment> tl = selectedAppointments.ToList<FIFPAppointment>();
                foreach (FIFPAppointment f in tl)
                {
                    f.IsOverlapped = false;
                }

                if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_SINGLEUNIT)
                {
                    bool overlappedFlag = CheckSelectedOverlapGroupForSingleUnit(tl);
                    ResultOnCheckOverlapOnSelectedPlans(overlappedFlag);
                }              
                else if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_TOWERWELLS)
                {
                    bool overlappedFlag = CheckSelectedOverlapGroupForSingleUnit(tl);
                    ResultOnCheckOverlapOnSelectedPlans(overlappedFlag);
                }
                else if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_TOWERPIPELINES)
                {

                    List<FIFPAppointment> t = new List<FIFPAppointment>();
                    foreach (FIFPAppointment p in tl)
                    {
                        if ( p.ActivityType != "MTN-SS")
                            t.Add(p);
                    }                   

                    bool overlappedFlag = CheckSelectedOverlapGroupForSingleUnit(t);
                    ResultOnCheckOverlapOnSelectedPlans(overlappedFlag);
                }
                else if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_WELLPIPELINES)
                {

                    List<FIFPAppointment> t = new List<FIFPAppointment>();
                    foreach (FIFPAppointment p in tl)
                    {
                        if (p.UnitType == FIFPConstants.UNITTYPE_WELL && p.ActivityType == "RIG")
                        {
                            t.Add(p);                           
                        }

                        if (p.UnitType == FIFPConstants.UNITTYPE_PIPELINE && p.ActivityType != "MTN-SS")
                        {
                            t.Add(p);
                        }
                    }

                    bool overlappedFlag = CheckSelectedOverlapGroupForSingleUnit(t);
                    ResultOnCheckOverlapOnSelectedPlans(overlappedFlag);
                }
                else if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_ALFATOWER)
                {
                    bool overlappedFlag = CheckSelectedOverlapGroupForSingleUnit(tl);
                    ResultOnCheckOverlapOnSelectedPlans(overlappedFlag);
                }
                //else if (selectedOverlapGroup.OverlapType == FIFPConstants.OVERLAP_TYPE_ALFATOWERWELLS)
                //{
                //    bool overlappedFlag = CheckSelectedOverlapGroupForSingleUnit(tl);
                //    ResultOnCheckOverlapOnSelectedPlans(overlappedFlag);
                //}


                SelectedAppointments = new ObservableCollection<FIFPAppointment>(tl);
                LoadOverlapPlans(SelectedAppointments);
            }
        }

        private bool CheckSelectedOverlapGroupForSingleUnit(List<FIFPAppointment> tl)
        {
             activityDictionary.Clear();
             foreach (FIFPAppointment p in tl)
             {
                activityDictionary.Add(p.PlanID, p);
             }

             List<PlanForSort> sortedList = GetSortList(tl);
             return FlagOverlappedPlans(sortedList);
        }       

        private void OverlapActivityChecked(CheckBox chkPlans)
        {
            if (chkPlans != null && chkPlans.Tag != null)
            {
                FIFPAppointment appointment = chkPlans.Tag as FIFPAppointment;
                if (appointment != null)
                {
                    CheckedAppointments.Add(appointment);
                    CheckApplySimposButtonStatus();                  
                } 
            }
        }

        private void OverlapActivityUnChecked(CheckBox chkPlans)
        {
            if (chkPlans != null && chkPlans.Tag != null)
            {
                FIFPAppointment appointment = chkPlans.Tag as FIFPAppointment;
                if (appointment != null)
                {
                    CheckedAppointments.Remove(appointment);
                    CheckApplySimposButtonStatus();
                }
            }        
        }

        private void CheckApplySimposButtonStatus()
        {
            if (CheckedAppointments.Count == 2)
            {
                if (CheckedAppointments[0].Division != CheckedAppointments[1].Division)
                {
                    if (CheckedAppointments[0].UnitType == FIFPConstants.UNITTYPE_TOWER)
                    {
                        if (CheckedAppointments[1].UnitType == FIFPConstants.UNITTYPE_WELL || CheckedAppointments[1].UnitType == FIFPConstants.UNITTYPE_STRING)
                        {
                            IsApplySimopsButtonEnabled = true;
                        }
                    }
                    else if (CheckedAppointments[0].UnitType == FIFPConstants.UNITTYPE_WELL || CheckedAppointments[0].UnitType == FIFPConstants.UNITTYPE_STRING)
                    {
                        if (CheckedAppointments[1].UnitType == FIFPConstants.UNITTYPE_TOWER)
                        {
                            IsApplySimopsButtonEnabled = true;
                        }
                    }
                }
            }
            else
            {
                IsApplySimopsButtonEnabled = false;
            }
        }

        private void ApplySimopsClicked(Button btnSave)
        {
            int i1 = SelectedAppointments.IndexOf(CheckedAppointments[0]);
            int i2 = SelectedAppointments.IndexOf(CheckedAppointments[1]);

            SelectedAppointments[i1].ModificationReason = "Apply Simops : " + SelectedAppointments[i1].UnitName + SelectedAppointments[i2].UnitName;
            SelectedAppointments[i1].IsAppliedSimops = true;
            SelectedAppointments[i2].ModificationReason = "Apply Simops : " + SelectedAppointments[i1].UnitName + SelectedAppointments[i2].UnitName;
            SelectedAppointments[i2].IsAppliedSimops = true;

            CheckedAppointments.Clear();
            IsApplySimopsButtonEnabled = false;

            SimopsAppliedGroup sg = new SimopsAppliedGroup(SelectedAppointments[i1], SelectedAppointments[i2]);          
            simopsAppliedGroups.Add(sg);
        }  

        private void SimposActivityChecked(CheckBox chkPlans)
        {
            if (chkPlans != null && chkPlans.Tag != null)
            {
                FIFPAppointment appointment = chkPlans.Tag as FIFPAppointment;
                if (appointment != null)
                {
                    checkedSimposAppointments.Add(appointment);
                    CheckCancelSimposButtonStatus();                  
                } 
            }
        }

        private void SimposActivityUnChecked(CheckBox chkPlans)
        {
            if (chkPlans != null && chkPlans.Tag != null)
            {
                FIFPAppointment appointment = chkPlans.Tag as FIFPAppointment;
                if (appointment != null)
                {
                    checkedSimposAppointments.Remove(appointment);
                    CheckCancelSimposButtonStatus();
                }
            }
        }

        private void CheckCancelSimposButtonStatus()
        {
            if (checkedSimposAppointments.Count == 2)
            {
                foreach (SimopsAppliedGroup sg in simopsAppliedGroups)
                {
                    if (sg.A == checkedSimposAppointments[0] && sg.B == checkedSimposAppointments[1])
                    {
                        IsCancelSimopsButtonEnabled = true;
                        selectedSimopsAppliedGroup = sg;
                        return;
                    }

                    if (sg.A == checkedSimposAppointments[1] && sg.B == checkedSimposAppointments[0])
                    {
                        IsCancelSimopsButtonEnabled = true;
                        selectedSimopsAppliedGroup = sg;
                        return;
                    }
                }
            }
            else
            {
                IsCancelSimopsButtonEnabled = false;
            }
            
        }

        private void CancelSimopsClicked(Button btnSave)
        { 
            int i1 = SelectedAppointments.IndexOf(checkedSimposAppointments[0]);
            int i2 = SelectedAppointments.IndexOf(checkedSimposAppointments[1]);
            
            SelectedAppointments[i1].ModificationReason = "";
            SelectedAppointments[i1].IsAppliedSimops = false;
            SelectedAppointments[i1].IsSelectedForCancelSimops = false;
            SelectedAppointments[i2].ModificationReason = "";
            SelectedAppointments[i2].IsAppliedSimops = false;
            SelectedAppointments[i2].IsSelectedForCancelSimops = false;

            checkedSimposAppointments.Clear();
            SimopsAppliedGroup sg = new SimopsAppliedGroup(SelectedAppointments[i1], SelectedAppointments[i2]);
            simopsAppliedGroups.Remove(selectedSimopsAppliedGroup);           
        } 
               

        //private void RemoveSelectedOverlapGroup(Button btnSelectedCheckOverlapGroup)
        //{
        //    OverlapGroups.Remove(selectedOverlapGroup);
        //}

        #endregion

        #region Methods

        private void InitOverlapGroupsForOverlapChecking()
        {
            if (OverlapGroups == null)
                OverlapGroups = new ObservableCollection<OverlapGroup>();
            else
                OverlapGroups.Clear();
        }

        private void PreOverlapCheck(List<FIFPAppointment> planList)
        {
            activityDictionary.Clear();
            foreach (FIFPAppointment p in planList)
            {
                activityDictionary.Add(p.PlanID, p);
                p.IsOverlapped = false;
            }
        }

        private void ResultOnCheckOverlapOnSelectedPlans(bool overlappedFlag)
        {
            if (overlappedFlag == true)
            {
                MessageBox.Show("Have Overlapped Plans.");
            }
            else
            {
                MessageBox.Show("No Overlapped Plans.");
            }
        }

        //update schedule
        private void LoadOverlapPlans(ObservableCollection<FIFPAppointment> plans)
        {
            //set red for overlapped plans, green for non-overlapped plans
            foreach (FIFPAppointment appointment in plans)
            {
                if (appointment.IsOverlapped == true)
                    appointment.Category = new Category("", new SolidColorBrush(Colors.Red));
                else
                    appointment.Category = new Category("", new SolidColorBrush(Colors.Green));
            }

            AppointmentGroup appointmentGroup = new AppointmentGroup();
            List<ResourceType> lstResources = new List<ResourceType>();
            List<Resource> unitList = new List<Resource>();
            ResourceType unitType = new ResourceType("Unit");
            unitType.DisplayName = "Unit";
            unitType.Name = "Unit";

            foreach (FIFPAppointment appointment in plans)
            {
                Resource resource = new Resource() { ResourceName = appointment.UnitName, DisplayName = appointment.UnitName, ResourceType = unitType.Name };
                //unitList.Add(resource);
                if (!unitList.Contains(resource))
                {
                    unitList.Add(resource);
                }
                appointment.Resources.Add(resource);
            }

            appointmentGroup.Appointments = new List<FIFPAppointment>(plans);
            unitType.Resources.AddRange(unitList);
            lstResources.Add(unitType);


            appointmentGroup.ResourceTypes = lstResources;
            appointmentGroup.UnitList = unitList;

            if (OnAppointmentDataCompleted != null)
                OnAppointmentDataCompleted(this, new CustomEventArgs<AppointmentGroup>(appointmentGroup));

        }
        #endregion

        #region Single Unit Type Overlap Checing

        private void OverlapCheckingOnSingleUnit()
        {
            if (selectedUnitType == null || selectedUnitType.Length == 0)
                MessageBox.Show("Please select unit type");
            else
            {
                InitOverlapGroupsForOverlapChecking();
                // progress bar show here ? 
                //overlap checking for the selected unit type
                bool checkResult = OverlapCheckingWithSelectedUnitType();
                if (checkResult == false)
                    MessageBox.Show("No Overlaps for " + selectedUnitType);
               
                if (SelectedAppointments != null)
                {
                        SelectedAppointments.Clear();
                        LoadOverlapPlans(SelectedAppointments);
                }
               
                // progress bar disappear here ?
            }
        }

        //check overlap for units with the selected unit type
        private bool OverlapCheckingWithSelectedUnitType()
        {
            //get plans with the selected unit type from all plans
            List<FIFPAppointment> plansWithUnitType = (from item in allPlans where item.UnitType == selectedUnitType
                                                       select item).ToList<FIFPAppointment>();         
               
            //get unit names with the selected unit type
            List<string> unitsWithTheSameUnitTypeList = getUnitsByUnitType(selectedUnitType);

            bool overallCheckingResult = false;
            foreach (string unitname in unitsWithTheSameUnitTypeList)
            {
                List<FIFPAppointment> plans = (from item in plansWithUnitType where item.UnitName == unitname
                                               select item).ToList<FIFPAppointment>();

                if (plans.Count > 1)
                {
                    bool tResult = CheckOverlapForOneUnit(plans);
                    if (tResult == true)
                        overallCheckingResult = true;
                }
            }

            return overallCheckingResult;
        }

        //check overlap plans for one unit
        private bool CheckOverlapForOneUnit(List<FIFPAppointment> planList)
        {
            PreOverlapCheck(planList);
            //check overlap
            List<PlanForSort> sortedList = GetSortList(planList);
            bool overlappedFlag = FlagOverlappedPlans(sortedList);

            //update binding list for the overlapped unit grid
            if (overlappedFlag == true)
            {
                OverlapGroups.Add(new OverlapGroup(planList[0].UnitName, FIFPConstants.OVERLAP_TYPE_SINGLEUNIT));
            }

            return overlappedFlag;
        }

        #endregion

        #region Tower and Wells Overlap Checking
        private void OverlapCheckingOnTowerWells()
        {
            InitOverlapGroupsForOverlapChecking();
            bool overallCheckingResult = false;
            
            //getUnitsByUnitType(.contains ("Tower"))
            List<string> allTowerNames = getUnitsByUnitType(FIFPConstants.UNITTYPE_TOWER);
            foreach (string towerName in allTowerNames)
            {
                List<FIFPAppointment> towerPlans = (from item in allPlans
                                                    where item.UnitName == towerName
                                                    select item).ToList<FIFPAppointment>();                
                if (towerPlans.Count > 0)
                {
                    List<string> relatedWellNames = getRelatedWellNames(towerName);
                    foreach (string wellName in relatedWellNames)
                    {
                        List<FIFPAppointment> wellPlans = (from item in allPlans
                                                           where item.UnitName == wellName
                                                           select item).ToList<FIFPAppointment>();

                        List<FIFPAppointment> overallPlans = new List<FIFPAppointment>();

                        if (wellPlans.Count > 0)
                        {
                            overallPlans.AddRange(towerPlans);
                            overallPlans.AddRange(wellPlans);

                            bool tResult = CheckOverlapForTowerWells(overallPlans, towerName, wellName);
                            if (tResult == true)
                                overallCheckingResult = true;
                        }
                    }
                }
            }

            if (overallCheckingResult == false)
                MessageBox.Show("No Overlaps for Tower and wells");
            
            if (SelectedAppointments != null)
            {
                    SelectedAppointments.Clear();
                    LoadOverlapPlans(SelectedAppointments);
            }

        }

        private bool CheckOverlapForTowerWells(List<FIFPAppointment> planList, string towerName, string wellName)
        {
            PreOverlapCheck(planList);
            //check overlap
            List<PlanForSort> sortedList = GetSortList(planList);
            bool overlappedFlag = FlagOverlappedPlans(sortedList);

            if (overlappedFlag == true)
            {
                string overlapGroupName = towerName + "|" + wellName;
                OverlapGroups.Add(new OverlapGroup(overlapGroupName, FIFPConstants.OVERLAP_TYPE_TOWERWELLS));
            }

            return overlappedFlag;
        }

        private List<string> getRelatedWellNames(string towerName)
        {
            List<string> l1 = new List<string>();
            l1.Add("well_1");
            l1.Add("well_2");

            List<string> l2 = new List<string>();
            l2.Add("well_3");

            List<string> result = new List<string>();

            if (towerName == "tower_1")
                result = l1;
            else
                result = l2;

            return result;
        }

        #endregion

        #region Tower and Pipelines Overlap Checking
        private void OverlapCheckingOnTowerPipelines()
        {
            InitOverlapGroupsForOverlapChecking();
            bool overallCheckingResult = false;

            //contains("tower")
            List<string> allTowerNames = getUnitsByUnitType(FIFPConstants.UNITTYPE_TOWER);
            foreach (string towerName in allTowerNames)
            {
                List<FIFPAppointment> towerPlans = (from item in allPlans
                                                    where item.UnitName == towerName
                                                    select item).ToList<FIFPAppointment>();

                if (towerPlans.Count > 0)
                {
                    List<string> relatedPipelineNames = getRelatedPipelineNames(towerName);
                    foreach (string pipelineName in relatedPipelineNames)
                    {
                        List<FIFPAppointment> pipelinePlans = (from item in allPlans where item.UnitName == pipelineName && item.ActivityType != "MTN-SS"
                                                               select item).ToList<FIFPAppointment>();

                        List<FIFPAppointment> overallPlans = new List<FIFPAppointment>();

                        if (pipelinePlans.Count > 0)
                        {
                            overallPlans.AddRange(towerPlans);
                            overallPlans.AddRange(pipelinePlans);

                            bool tResult = CheckOverlapForTowerPipelines(overallPlans, towerName, pipelineName);
                            if (tResult == true)
                                overallCheckingResult = true;
                        }
                    }
                }
            }

            if (overallCheckingResult == false)
                MessageBox.Show("No Overlaps for Tower and Pipelines");
            if (SelectedAppointments != null)
            {
                    SelectedAppointments.Clear();
                    LoadOverlapPlans(SelectedAppointments);
            }
           
        }

        private bool CheckOverlapForTowerPipelines(List<FIFPAppointment> planList, string towerName, string pipelineName)
        {
            PreOverlapCheck(planList);
            //check overlap
            List<PlanForSort> sortedList = GetSortList(planList);
            bool overlappedFlag = FlagOverlappedPlans(sortedList);

            if (overlappedFlag == true)
            {
                string overlapGroupName = towerName + "|" + pipelineName;
                OverlapGroups.Add(new OverlapGroup(overlapGroupName, FIFPConstants.OVERLAP_TYPE_TOWERPIPELINES));
            }

            return overlappedFlag;
        }

        //dummy data
        private List<string> getRelatedPipelineNames(string towerName)
        {
            List<string> l1 = new List<string>();
            l1.Add("pipeline_1");
            l1.Add("pipeline_2");

            List<string> l2 = new List<string>();
            l2.Add("pipeline_3");

            List<string> result = new List<string>();

            if (towerName == "tower_1")
                result = l1;
            else
                result = l2;

            return result;
        }


        #endregion

        #region Well and Pipelines Overlap Checking
        private void OverlapCheckingOnWelPipelines()
        {
            InitOverlapGroupsForOverlapChecking();
            bool overallCheckingResult = false;

            List<FIFPAppointment> wellPlanssWithRigActivity = (from item in allPlans
                                                               where item.UnitType == FIFPConstants.UNITTYPE_WELL && item.ActivityType == "RIG"
                                                               select item).ToList<FIFPAppointment>();

            List<string> wellNamesWithRigActivity = (from item in wellPlanssWithRigActivity                                                     
                                                     select item.UnitName).Distinct().ToList<string>();

            foreach (string wellName in wellNamesWithRigActivity)
            {
                List<FIFPAppointment> wellPlans = (from item in wellPlanssWithRigActivity where item.UnitName == wellName
                                                   select item).ToList<FIFPAppointment>();
                
                //string connectedTowerName = getConnectedTower();
                string connectedTowerName = "tower_1";
                List<string> relatedPipelineNames = getRelatedPipelineNames(connectedTowerName);

                foreach (string pipelineName in relatedPipelineNames)
                {
                    List<FIFPAppointment> pipelinePlans = (from item in allPlans
                                                           where item.UnitName == pipelineName && item.ActivityType != "MTN-SS"
                                                           select item).ToList<FIFPAppointment>();

                    List<FIFPAppointment> overallPlans = new List<FIFPAppointment>();

                    if (pipelinePlans.Count > 0)
                    {
                        overallPlans.AddRange(wellPlans);
                        overallPlans.AddRange(pipelinePlans);

                        bool tResult = CheckOverlapForWellPipelines(overallPlans,connectedTowerName,wellName,pipelineName);
                        if (tResult == true)
                            overallCheckingResult = true;
                    }
                }
            }


            if (overallCheckingResult == false)
                MessageBox.Show("No Overlaps for Tower and Pipelines");
            if (SelectedAppointments != null)
            {
                SelectedAppointments.Clear();
                LoadOverlapPlans(SelectedAppointments);
            }
        }

        private bool CheckOverlapForWellPipelines(List<FIFPAppointment> planList, string connectedTowerName, 
                                                   string wellName, string pipelineName)
        {
            PreOverlapCheck(planList);
            //check overlap
            List<PlanForSort> sortedList = GetSortList(planList);
            bool overlappedFlag = FlagOverlappedPlans(sortedList);

            if (overlappedFlag == true)
            {
                string overlapGroupName = connectedTowerName + "|" + wellName + "|" + pipelineName;
                OverlapGroups.Add(new OverlapGroup(overlapGroupName, FIFPConstants.OVERLAP_TYPE_WELLPIPELINES));
            }
            return overlappedFlag;
        }

        #endregion

        #region Alfa Tower Overlap Checking
        //alfa tower overlap checking button clicked
        private void OverlapCheckingOnAlfaTower()
        {
            InitOverlapGroupsForOverlapChecking();

            List<FIFPAppointment> plansOfAlfaTowers = (from item in allPlans where item.UnitType == FIFPConstants.UNITTYPE_ALFATOWER
                                                       select item).ToList<FIFPAppointment>();

            List<string> alfaTowerAllUnitNames = getUnitsByUnitType(FIFPConstants.UNITTYPE_ALFATOWER);
            List<string> alfaTowerSingleUnitNames = getAlfaTowerNamesWithoutA(alfaTowerAllUnitNames);

            bool checkResult = true;           

            foreach (string unitname in alfaTowerSingleUnitNames)
            {
                List<FIFPAppointment> plans = (from item in plansOfAlfaTowers
                                               where item.UnitName == unitname || (item.UnitName == unitname + "A")
                                               select item).ToList<FIFPAppointment>();

                if (plans.Count > 1)
                    checkResult = CheckOverlapForAlfaTower(plans, unitname);

            }

            if (checkResult == false)
                MessageBox.Show("No Overlaps for Alfa towers" );
           
            if (SelectedAppointments != null)
            {
                    SelectedAppointments.Clear();
                    LoadOverlapPlans(SelectedAppointments);
            }
            

        }

        private bool CheckOverlapForAlfaTower(List<FIFPAppointment> planList, string towerName)
        {
            PreOverlapCheck(planList);
            //check overlap
            List<PlanForSort> sortedList = GetSortList(planList);
            bool overlappedFlag = FlagOverlappedPlans(sortedList);

            if (overlappedFlag == true)
            {
                //update binding list for the overlapped unit grid
                string overlapGroupName = towerName + "|" + towerName + "A";                                         
                OverlapGroups.Add(new OverlapGroup(overlapGroupName, FIFPConstants.OVERLAP_TYPE_ALFATOWER));
            }
            return overlappedFlag;
        }

        private List<string> getAlfaTowerNamesWithoutA(List<string> alfaTowerAllUnitNames)
        {
            List<string> plans = (from item in alfaTowerAllUnitNames
                                  where ! item.EndsWith("A")
                                  select item).ToList<string>();

            return plans;
        }
        #endregion      

        //#region Alfa Tower and Wells Overlap Checking
        //private void OverlapCheckingOnAlfaTowerWells()
        //{
        //    InitOverlapGroupsForOverlapChecking();

        //    List<string> allAlfaTowerNames = getUnitsByUnitType(FIFPConstants.UNITTYPE_ALFATOWER);
        //    List<string> alfaTowerSingleUnitNames = getAlfaTowerNamesWithoutA(allAlfaTowerNames);

        //    bool overallCheckingResult = false;
        //    foreach (string towerName in alfaTowerSingleUnitNames)
        //    {
        //        List<FIFPAppointment> towerPlans = (from item in allPlans
        //                                            where item.UnitName == towerName || (item.UnitName == towerName + "A")
        //                                            select item).ToList<FIFPAppointment>();
        //        if (towerPlans.Count > 0)
        //        {
        //            List<string> relatedWellNames = getRelatedWellNames(towerName);
        //            List<string> relatedWellNamesA = getRelatedWellNames(towerName + "A");
        //            relatedWellNames.AddRange(relatedWellNamesA);

        //            foreach (string wellName in relatedWellNames)
        //            {
        //                List<FIFPAppointment> wellPlans = (from item in allPlans
        //                                                   where item.UnitName == wellName
        //                                                   select item).ToList<FIFPAppointment>();
        //                List<FIFPAppointment> overallPlans = new List<FIFPAppointment>();
        //                if (wellPlans.Count > 0)
        //                {
        //                    overallPlans.AddRange(towerPlans);
        //                    overallPlans.AddRange(wellPlans);

        //                    bool tResult = CheckOverlapForAlfaTowerWells(overallPlans, towerName, wellName);
        //                    if (tResult == true)
        //                        overallCheckingResult = true;
        //                }
        //            }
        //        }
        //    }

        //    if (overallCheckingResult == false)
        //        MessageBox.Show("No Overlaps for Tower and wells");
            
            
        //    if (SelectedAppointments != null)
        //    {
        //        SelectedAppointments.Clear();
        //        LoadOverlapPlans(SelectedAppointments);
        //    }
            

        //}


        //private bool CheckOverlapForAlfaTowerWells(List<FIFPAppointment> planList, string towerName, string wellName)
        //{
        //    //Init activitiDictionary
        //    activityDictionary.Clear();
        //    foreach (FIFPAppointment p in planList)
        //    {
        //        activityDictionary.Add(p.PlanID, p);
        //    }
        //    //check overlap
        //    List<PlanForSort> sortedList = GetSortList(planList);
        //    bool overlappedFlag = FlagOverlappedPlans(sortedList);
        //    if (overlappedFlag == true)
        //    {
        //        string overlapGroupName = towerName + "(A)|" + wellName;
        //        OverlapGroups.Add(new OverlapGroup(overlapGroupName, FIFPConstants.OVERLAP_TYPE_ALFATOWERWELLS));
        //    }
        //    return overlappedFlag;
        //}

        //#endregion

        #region Overlap Checking algrithom
        //input  : activities for one unit
        //output : a list filled with PlanForSort,  and it is sorted with all start date and end date
        private List<PlanForSort> GetSortList(List<FIFPAppointment> list)
        {
            List<PlanForSort> sortedList = new List<PlanForSort>();
            //create the list for sort
            foreach (FIFPAppointment a in list)
            {
                //flag = 1  means it is a  start date
                //flag = -1 means it is an end date
                sortedList.Add(new PlanForSort(a.PlanID, a.Start, 1));
                sortedList.Add(new PlanForSort(a.PlanID, a.End, -1));
            }
            //sort
            sortedList.Sort();
            return sortedList;
        }

        //input  : sorted list of activitiesforplan for one unit        
        private bool FlagOverlappedPlans(List<PlanForSort> list)
        {
            bool returnFlag = false;
            int sumFlag = 0;
            List<FIFPAppointment> tempList = new List<FIFPAppointment>();

            foreach (PlanForSort a in list)
            {
                //sumflag == 0 means it is a new overlap group, so 
                //create a new list for activities in this overlap group
                //add the current activity
                //add the list to the result 
                if (sumFlag == 0)
                {
                    List<FIFPAppointment> l = new List<FIFPAppointment>();
                    l.Add(activityDictionary[a.id]);

                    sumFlag += a.flag;

                    if (tempList.Count > 1)
                    {
                        foreach (FIFPAppointment p in tempList)
                        {
                            p.IsOverlapped = true;
                        }
                        returnFlag = true;
                    }
                    tempList = l;
                }
                //sumflag != 0 means it is in the process of the overlap group
                else
                {
                    if (a.flag == 1)
                    {
                        tempList.Add(activityDictionary[a.id]);
                    }

                    sumFlag += a.flag;

                    if (sumFlag == 0)
                    {
                        if (tempList.Count > 1)
                        {
                            foreach (FIFPAppointment p in tempList)
                            {
                                p.IsOverlapped = true;
                            }
                            returnFlag = true;
                        }
                    }
                }
            }

            return returnFlag;
        }


        #endregion

        #region dummy data
        private void LoadUnitType()
        {
            UnitTypeList.Add(FIFPConstants.UNITTYPE_COMPRESSOR);
            UnitTypeList.Add(FIFPConstants.UNITTYPE_SEPERATOR);
            UnitTypeList.Add(FIFPConstants.UNITTYPE_RADIAL);
            UnitTypeList.Add(FIFPConstants.UNITTYPE_TOWER);
            UnitTypeList.Add(FIFPConstants.UNITTYPE_WELL);
            UnitTypeList.Add(FIFPConstants.UNITTYPE_STRING);

        }

        private void getAllPlans()
        {
            FIFPAppointment appointment1 = new FIFPAppointment();
            appointment1.PlanID = 1;
            appointment1.Subject = "MTN-MM";
            appointment1.Division = "MTN";
            appointment1.UnitName = "seperator_1";
            appointment1.UnitType = FIFPConstants.UNITTYPE_SEPERATOR;
            appointment1.ActivityType = "InstallDevice";
            appointment1.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment1.OriginalStart = appointment1.Start = new DateTime(2013, 1, 5);
            appointment1.OriginalEnd = appointment1.End = new DateTime(2013, 1, 20);
            appointment1.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment2 = new FIFPAppointment();
            appointment2.PlanID = 2;
            appointment2.Subject = "MTN-MM";
            appointment2.Division = "MTN";
            appointment2.UnitName = "seperator_1";
            appointment2.UnitType = FIFPConstants.UNITTYPE_SEPERATOR;
            appointment2.ActivityType = "InstallDevice";
            appointment2.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment2.OriginalStart = appointment2.Start = new DateTime(2013, 1, 10);
            appointment2.OriginalEnd = appointment2.End = new DateTime(2013, 1, 15);
            appointment2.Category = new Category("Active", new SolidColorBrush(Colors.Red));


            FIFPAppointment appointment3 = new FIFPAppointment();
            appointment3.PlanID = 3;
            appointment3.Subject = "MTN-MM";
            appointment3.Division = "MTN";
            appointment3.UnitName = "seperator_1";
            appointment3.UnitType = FIFPConstants.UNITTYPE_SEPERATOR;
            appointment3.ActivityType = "InstallDevice";
            appointment3.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment3.OriginalStart = appointment3.Start = new DateTime(2013, 1, 1);
            appointment3.OriginalEnd = appointment3.End = new DateTime(2013, 1, 8);
            appointment3.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment4 = new FIFPAppointment();
            appointment4.PlanID = 4;
            appointment4.Subject = "MTN-MM";
            appointment4.Division = "MTN";
            appointment4.UnitName = "seperator_1";
            appointment4.UnitType = FIFPConstants.UNITTYPE_SEPERATOR;
            appointment4.ActivityType = "InstallDevice";
            appointment4.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment4.OriginalStart = appointment4.Start = new DateTime(2013, 1, 15);
            appointment4.OriginalEnd = appointment4.End = new DateTime(2013, 1, 25);
            appointment4.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment5 = new FIFPAppointment();
            appointment5.PlanID = 5;
            appointment5.Subject = "MTN-MM";
            appointment5.Division = "MTN";
            appointment5.UnitName = "seperator_1";
            appointment5.UnitType = FIFPConstants.UNITTYPE_SEPERATOR;
            appointment5.ActivityType = "InstallDevice";
            appointment5.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment5.OriginalStart = appointment5.Start = new DateTime(2013, 1, 26);
            appointment5.OriginalEnd = appointment5.End = new DateTime(2013, 1, 29);
            appointment5.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment6 = new FIFPAppointment();
            appointment6.PlanID = 6;
            appointment6.Subject = "MTN-MM";
            appointment6.Division = "MTN";
            appointment6.UnitName = "seperator_1";
            appointment6.UnitType = FIFPConstants.UNITTYPE_SEPERATOR;
            appointment6.ActivityType = "InstallDevice";
            appointment6.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment6.OriginalStart = appointment6.Start = new DateTime(2013, 2, 5);
            appointment6.OriginalEnd = appointment6.End = new DateTime(2013, 2, 15);
            appointment6.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment7 = new FIFPAppointment();
            appointment7.PlanID = 7;
            appointment7.Subject = "MTN-MM";
            appointment7.Division = "MTN";
            appointment7.UnitName = "seperator_1";
            appointment7.UnitType = FIFPConstants.UNITTYPE_SEPERATOR;
            appointment7.ActivityType = "InstallDevice";
            appointment7.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment7.OriginalStart = appointment7.Start = new DateTime(2013, 3, 3);
            appointment7.OriginalEnd = appointment7.End = new DateTime(2013, 5, 3);
            appointment7.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment8 = new FIFPAppointment();
            appointment8.PlanID = 8;
            appointment8.Subject = "MTN-MM";
            appointment8.Division = "MTN";
            appointment8.UnitName = "seperator_1";
            appointment8.UnitType = FIFPConstants.UNITTYPE_SEPERATOR;
            appointment8.ActivityType = "InstallDevice";
            appointment8.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment8.OriginalStart = appointment8.Start = new DateTime(2013, 2, 10);
            appointment8.OriginalEnd = appointment8.End = new DateTime(2013, 2, 20);
            appointment8.Category = new Category("Active", new SolidColorBrush(Colors.Red));


            allPlans = new List<FIFPAppointment>();
            allPlans.Add(appointment1);
            allPlans.Add(appointment2);
            allPlans.Add(appointment3);
            allPlans.Add(appointment4);
            allPlans.Add(appointment5);
            allPlans.Add(appointment6);
            allPlans.Add(appointment7);
            allPlans.Add(appointment8);


            FIFPAppointment appointment9 = new FIFPAppointment();
            appointment9.PlanID = 9;
            appointment9.Subject = "MTN-MM";
            appointment9.Division = "MTN";
            appointment9.UnitName = "tower_1";
            //appointment9.IsAlfaTower = false;
            appointment9.UnitType = FIFPConstants.UNITTYPE_TOWER;
            appointment9.ActivityType = "InstallDevice";
            appointment9.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment9.OriginalStart = appointment9.Start = new DateTime(2013, 1, 5);
            appointment9.OriginalEnd = appointment9.End = new DateTime(2013, 1, 20);
            appointment9.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment10 = new FIFPAppointment();
            appointment10.PlanID = 10;
            appointment10.Subject = "MTN-MM";
            appointment10.Division = "MTN";
            appointment10.UnitName = "tower_1";
            //appointment10.IsAlfaTower = false;
            appointment10.UnitType = FIFPConstants.UNITTYPE_TOWER;
            appointment10.ActivityType = "InstallDevice";
            appointment10.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment10.OriginalStart = appointment10.Start = new DateTime(2013, 1, 10);
            appointment10.OriginalEnd = appointment10.End = new DateTime(2013, 1, 15);
            appointment10.Category = new Category("Active", new SolidColorBrush(Colors.Red));


            FIFPAppointment appointment11 = new FIFPAppointment();
            appointment11.PlanID = 11;
            appointment11.Subject = "MTN-MM";
            appointment11.Division = "MTN";
            appointment11.UnitName = "tower_1";
            //appointment11.IsAlfaTower = false;
            appointment11.UnitType = FIFPConstants.UNITTYPE_TOWER;
            appointment11.ActivityType = "InstallDevice";
            appointment11.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment11.OriginalStart = appointment11.Start = new DateTime(2013, 1, 1);
            appointment11.OriginalEnd = appointment11.End = new DateTime(2013, 1, 8);
            appointment11.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment12 = new FIFPAppointment();
            appointment12.PlanID = 12;
            appointment12.Subject = "MTN-MM";
            appointment12.Division = "MTN";
            appointment12.UnitName = "tower_1";
            //appointment12.IsAlfaTower = false;
            appointment12.UnitType = FIFPConstants.UNITTYPE_TOWER;
            appointment12.ActivityType = "InstallDevice";
            appointment12.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment12.OriginalStart = appointment12.Start = new DateTime(2013, 1, 15);
            appointment12.OriginalEnd = appointment12.End = new DateTime(2013, 1, 25);
            appointment12.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            allPlans.Add(appointment9);
            allPlans.Add(appointment10);
            allPlans.Add(appointment11);
            allPlans.Add(appointment12);


            //wells
            FIFPAppointment appointment21 = new FIFPAppointment();
            appointment21.PlanID = 21;
            appointment21.Subject = "MTN-MM";
            appointment21.Division = "MTN";
            appointment21.UnitName = "well_1";
            //appointment12.IsAlfaTower = false;
            appointment21.UnitType = FIFPConstants.UNITTYPE_WELL;
            appointment21.ActivityType = "InstallDevice";
            appointment21.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment21.OriginalStart = appointment21.Start = new DateTime(2013, 1, 19);
            appointment21.OriginalEnd = appointment21.End = new DateTime(2013, 1, 28);
            //appointment21.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment22 = new FIFPAppointment();
            appointment22.PlanID = 22;
            appointment22.Subject = "MTN-MM";
            appointment22.Division = "MTN";
            appointment22.UnitName = "well_1";
            //appointment12.IsAlfaTower = false;
            appointment22.UnitType = FIFPConstants.UNITTYPE_WELL;
            appointment22.ActivityType = "InstallDevice";
            appointment22.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment22.OriginalStart = appointment22.Start = new DateTime(2013, 1, 1);
            appointment22.OriginalEnd = appointment22.End = new DateTime(2013, 1, 15);
            //appointment21.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment23 = new FIFPAppointment();
            appointment23.PlanID = 23;
            appointment23.Subject = "MTN-MM";
            appointment23.Division = "MTN";
            appointment23.UnitName = "well_1";
            //appointment12.IsAlfaTower = false;
            appointment23.UnitType = FIFPConstants.UNITTYPE_WELL;
            appointment23.ActivityType = "InstallDevice";
            appointment23.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment23.OriginalStart = appointment23.Start = new DateTime(2013, 3, 1);
            appointment23.OriginalEnd = appointment23.End = new DateTime(2013, 4, 1);
            //appointment21.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment24 = new FIFPAppointment();
            appointment24.PlanID = 24;
            appointment24.Subject = "MTN-MM";
            appointment24.Division = "Drilling";
            appointment24.UnitName = "well_1";
            appointment24.UnitType = FIFPConstants.UNITTYPE_WELL;
            appointment24.ActivityType = "RIG";
            appointment24.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment24.OriginalStart = appointment24.Start = new DateTime(2013, 1, 1);
            appointment24.OriginalEnd = appointment24.End = new DateTime(2013, 1, 10);

            allPlans.Add(appointment21);
            allPlans.Add(appointment22);
            allPlans.Add(appointment23);
            allPlans.Add(appointment24);



            //pipelines
            FIFPAppointment appointment31 = new FIFPAppointment();
            appointment31.PlanID = 31;
            appointment31.Subject = "MTN-MM";
            appointment31.Division = "MTN";
            appointment31.UnitName = "pipeline_1";
            //appointment12.IsAlfaTower = false;
            appointment31.UnitType = FIFPConstants.UNITTYPE_PIPELINE;
            appointment31.ActivityType = "InstallDevice";
            appointment31.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment31.OriginalStart = appointment31.Start = new DateTime(2013, 1, 19);
            appointment31.OriginalEnd = appointment31.End = new DateTime(2013, 1, 28);
            //appointment21.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment32 = new FIFPAppointment();
            appointment32.PlanID = 32;
            appointment32.Subject = "MTN-MM";
            appointment32.Division = "MTN";
            appointment32.UnitName = "pipeline_1";
            //appointment12.IsAlfaTower = false;
            appointment32.UnitType = FIFPConstants.UNITTYPE_PIPELINE;
            appointment32.ActivityType = "InstallDevice";
            appointment32.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment32.OriginalStart = appointment32.Start = new DateTime(2013, 1, 1);
            appointment32.OriginalEnd = appointment32.End = new DateTime(2013, 1, 10);
            //appointment21.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment33 = new FIFPAppointment();
            appointment33.PlanID = 33;
            appointment33.Subject = "MTN-MM";
            appointment33.Division = "MTN";
            appointment33.UnitName = "pipeline_1";
            //appointment12.IsAlfaTower = false;
            appointment33.UnitType = FIFPConstants.UNITTYPE_WELL;
            appointment33.ActivityType = "MTN-SS";
            appointment33.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment33.OriginalStart = appointment33.Start = new DateTime(2013, 1, 12);
            appointment33.OriginalEnd = appointment33.End = new DateTime(2013, 1, 18);
            //appointment21.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            allPlans.Add(appointment31);
            allPlans.Add(appointment32);
            allPlans.Add(appointment33);


            FIFPAppointment appointment13 = new FIFPAppointment();
            appointment13.PlanID = 13;
            appointment13.Subject = "MTN-MM";
            appointment13.Division = "MTN";
            appointment13.UnitName = "tower_2";
            //appointment13.IsAlfaTower = false;
            appointment13.UnitType = FIFPConstants.UNITTYPE_TOWER;
            appointment13.ActivityType = "InstallDevice";
            appointment13.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment13.OriginalStart = appointment13.Start = new DateTime(2013, 1, 15);
            appointment13.OriginalEnd = appointment13.End = new DateTime(2013, 1, 25);
            appointment13.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment14 = new FIFPAppointment();
            appointment14.PlanID = 14;
            appointment14.Subject = "MTN-MM";
            appointment14.Division = "MTN";
            appointment14.UnitName = "tower_2";
            //appointment14.IsAlfaTower = false;
            appointment14.UnitType = FIFPConstants.UNITTYPE_TOWER;
            appointment14.ActivityType = "InstallDevice";
            appointment14.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment14.OriginalStart = appointment14.Start = new DateTime(2013, 1, 15);
            appointment14.OriginalEnd = appointment14.End = new DateTime(2013, 1, 25);
            appointment14.Category = new Category("Active", new SolidColorBrush(Colors.Red));


            allPlans.Add(appointment13);
            allPlans.Add(appointment14);



            FIFPAppointment appointment15 = new FIFPAppointment();
            appointment15.PlanID = 15;
            appointment15.Subject = "MTN-MM";
            appointment15.Division = "MTN";
            appointment15.UnitName = "tower_4";
            //appointment15.IsAlfaTower = true;
            appointment15.UnitType = FIFPConstants.UNITTYPE_ALFATOWER;
            appointment15.ActivityType = "InstallDevice";
            appointment15.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment15.OriginalStart = appointment15.Start = new DateTime(2013, 1, 15);
            appointment15.OriginalEnd = appointment15.End = new DateTime(2013, 1, 25);
            appointment15.Category = new Category("Active", new SolidColorBrush(Colors.Red));

            FIFPAppointment appointment16 = new FIFPAppointment();
            appointment16.PlanID = 16;
            appointment16.Subject = "MTN-MM";
            appointment16.Division = "MTN";
            appointment16.UnitName = "tower_4A";
            //appointment16.IsAlfaTower = true;
            appointment16.UnitType = FIFPConstants.UNITTYPE_ALFATOWER;
            appointment16.ActivityType = "InstallDevice";
            appointment16.FluidType = FIFPConstants.FLUIDTYPE_OIL;
            appointment16.OriginalStart = appointment16.Start = new DateTime(2013, 1, 15);
            appointment16.OriginalEnd = appointment16.End = new DateTime(2013, 1, 25);
            appointment16.Category = new Category("Active", new SolidColorBrush(Colors.Red));


            allPlans.Add(appointment15);
            allPlans.Add(appointment16);
        }

        //Need to Change
        private List<string> getUnitsByUnitType(string unitType)
        {
            List<string> lS = new List<string>();
            lS.Add(allPlans[0].UnitName);
            lS.Add("separator_2");

            List<string> lT = new List<string>();
            lT.Add("tower_1");
            lT.Add("tower_2");
            lT.Add("tower_3");

            List<string> lTA = new List<string>();
            lTA.Add("tower_4");
            lTA.Add("tower_4A");

            List<string> result = lS;

            if (unitType == FIFPConstants.UNITTYPE_TOWER)
                result = lT;
            else if (unitType == FIFPConstants.UNITTYPE_ALFATOWER)
                result = lTA;
            else if (unitType == FIFPConstants.UNITTYPE_WELL)
                result = new List<string>();

            return result;

        }

       
        #endregion
    }
}
